package com.reer;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-4-26
 * Time: 下午3:30
 */
public class ListActivity extends Activity {
    public static final int DATA_NOTIFY_MSG = 1;
    private static final String TAG = "REER-READER";
    public static String listKey = "";

    public static Handler mHandler;
    private SimpleAdapter listAdapter;
    private ListView mListView;
    private int[] colors = new int[]{Color.parseColor("#C0FF3E"), Color.WHITE};
    private List<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();

    private ListActivityController controller;
    private ViewFlipper viewFlipper;

    private TextView mTitleTextView;
    private TextView mContentTextView;
    private Button mBackListBtn;
    private Button mRefreshListBtn;

    private String contentId;

    private static final int DIALOG_PROGRESS = 1;
    private ProgressDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.list_activity);
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == DATA_NOTIFY_MSG) {
                    changeData(listKey);
                }
            }
        };
        viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
        mTitleTextView=(TextView)findViewById(R.id.content_title);
        mContentTextView=(TextView)findViewById(R.id.content_text);
        mBackListBtn=(Button)findViewById(R.id.back_to_list_btn);
        mBackListBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewFlipper.showPrevious();
            }
        });
       mRefreshListBtn=(Button)findViewById(R.id.refresh_list_btn);
        mRefreshListBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                changeData(listKey);
                showDialog(DIALOG_PROGRESS);
                controller.requestContentList();
            }
        });

        controller=new ListActivityController(this);

        initListView();
    }

    private void initData(int count){
        listData.clear();
        for(int i=0;i<count;i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("itemTitle", "Title "+i);
            map.put("itemDesc", "this is desc "+i);
            map.put("itemId", listKey+i);
            listData.add(map);
        }
        listAdapter.notifyDataSetChanged();
    }

    private void initListView() {
        mListView = (ListView) this.findViewById(R.id.query_list_view);
        listAdapter = new SimpleAdapter(this, listData, R.layout.list_activity_item,
                new String[]{"itemTitle", "itemDesc"},
                new int[]{R.id.item_title, R.id.item_desc,}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                int colorPos = position % colors.length;
                view.setBackgroundColor(colors[colorPos]);
                return view;
            }
        };
        mListView.setAdapter(listAdapter);
        mListView.setOnItemClickListener(new ListViewClickedListener());
    }

    public void changeData(String id) {
        showDialog(DIALOG_PROGRESS);
        Log.d(TAG, "id:" + id);
        initData(Integer.parseInt(id) * 5);
        hideProgressDialog();
    }

    class ListViewClickedListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String itemId = listData.get(i).get("itemId").toString();
            Log.d(TAG, itemId);
            contentId=itemId;
            showContent();
        }
    }

    private void showContent(){
        viewFlipper.showNext();
        mTitleTextView.setText("this is "+contentId);
        mContentTextView.setText(Html.fromHtml("<p color='red'>content </p>"));
    }



    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
